clear all 
set gr on

*cd /home/projects/cedsei/DODS/

{ // Graph of bunching over the year. Taken from /do/temp20210526_bunch.do.
use "$datapath/A1_varseldata.dta",clear

collapse (mean) antvars novars_tot (count) persid (min) ankomst_dat (first) plantid, by(varselid)
replace novars_tot = round(novars_tot)

bys plantid (ankomst_dat) : gen pr_oneyear = (ankomst_dat[_n+1] - ankomst_dat)<=365

bys plantid (ankomst_dat) : gen nvals = _n
bys plantid (ankomst_dat) :  egen maxnvals = max(nvals)

bys plantid (ankomst_dat) : gen newspell = 1 if nvals==1
bys plantid (ankomst_dat) : replace newspell = 1 if pr_oneyear==1 & pr_oneyear[_n-1]==0 & nvals!=1 & newspell==.

bys plantid (ankomst_dat) :  replace newspell = 1 if maxnvals==nvals & (ankomst_dat - ankomst_dat[_n-1])>365 & newspell==.

bys plantid (ankomst_dat) : gen spellnr = sum(newspell)
egen spelllopnr = group(plantid spellnr)

collapse (sum) antvars, by(spelllopnr)
collapse (count) spelllopnr, by(antvars)
keep if inrange(antvars,15,40)
egen tot = total(spelllopnr)
sum tot
gen avg = spelllopnr/tot

twoway (connected avg antvars if inrange(antvars,5,50), lcolor(orange_red) mc(orange_red))  ///
,xline(25, lpattern(dash) lcolor(gs7) lstyle(foreground)) ///
graphregion(color(white)) ytitle("Share of layoffs") xtitle("Number of notified workers over a year") yl(0(0.02)0.1)
graph export "$agegraphpath/Bunching/bunching_0.pdf",replace
}


use "$datapath/A1_varseldata.dta",clear


gen date = ym_inkom_dat
merge 1:1 persid firmid date using "$datapath/A2_tenure.dta"
drop if _merge==2
drop _merge

{ // Merge on date of birth
*===============================================================================
merge m:1 persid using "$datapath/A0_fodelsedatum.dta"

*Missing birth date for 513 people
lab var birth 	"Date of birth"

drop if _merge==2
drop  	_merge

gen age = (date - birth)/12
}
*

gen diff = list_dat - ankomst_dat
replace diff = -10 if diff<-10
replace diff = 365 if diff>=265 & !mi(diff)
hist diff, width(5) percent note("Note:binwidth 5, tunctaed at -10 and 365 days") title("list date - report date")
hist diff if varselorsak<5, width(5) percent note("Note:binwidth 5, tunctaed at -10 and 365 days") title("list date - report date") subtitle("no bankrupty or clousers") name(b,replace)
tab varselorsak

gen Ddiff=diff==0
sum Ddiff if !inrange(varselorsak,5,6)


gen 	de_jure_NT = .
replace de_jure_NT = 30  	if inrange(tenure4,0,23) 
replace de_jure_NT = 60  	if inrange(tenure4,24,47) 
replace de_jure_NT = 90   	if inrange(tenure4,48,71) 
replace de_jure_NT = 120 	if inrange(tenure4,72,95) 
replace de_jure_NT = 150 	if inrange(tenure4,96,119) 
replace de_jure_NT = 180   	if tenure4>=120 & tenure4!=. 
replace de_jure_NT = 365 	if  age>=55 

gen complier = de_jure_NT<=120 

save "$datapath/F2_bunchingdata", replace

clear
use  "$datapath/F2_bunchingdata"

/*
*Indicate compliers (i.e those workers affected by the varsellaw)
*subtract from the 4 months the time between intial report to PES and when the list is sent in
gen 	de_jure_NT_Vlaw = de_jure_NT
gen test = 120 - (list_dat - ankomst_dat) 
gen test2 = max(test, de_jure_NT_Vlaw)
replace de_jure_NT_Vlaw = test2 
gen complier = de_jure_NT_Vlaw> de_jure_NT
*/

collapse (mean) complier antvars novars_tot (count) persid (min) varselorsak ankomst_dat (first) plantid, by(varselid)
replace novars_tot = round(novars_tot)
compare antvars novars_tot 

keep if inrange(antvars,15,40) 
gen closings = varselorsak>=5

tempfile a0
save 	`a0', replace

tempfile bunch1
foreach num of numlist 1(1)101 {
	di "Run `num'"
	qui {
	clear 
	use `a0'
	
	if `num'>1 {
		bsample //, strata(complier2)
	}
	bys closings: gen tot =_N

	collapse (count) varselid , by(antvars tot closings)
	sum tot
	gen avg = varselid/tot
	sort closings antvars
	keep if inrange(antvars,22,25) 
	gen run = `num'
	if `num'>1 {
		append using `bunch1'		
	}
	save `bunch1', replace
	}
}

gen temp = avg if antvars==25 & closings==1 
egen cf = max(temp), by(run)
collapse (sum) avg, by(closings run cf)
gen temp = avg if closings==1 
egen mtem = max(temp), by(run)
gen excessmass = (avg - mtem)/cf 
drop mtem temp
gegen sd_mass = sd(excessmass) if run>1, by(closings)
egen temp=max(sd_mass), by(closings)
replace sd_mass = temp if mi(sd_mass)
keep if run==1 & closings==0
sum excessmass
local bu = round(r(mean)*1000)/1000
sum sd_mass
local bu_sd = round(r(mean)*1000)/1000

clear
use `a0'

bys closings: gen tot =_N

collapse (count) varselid , by(antvars tot closings)
sum tot
gen avg = varselid/tot
sort closings antvars

sum avg if antvars==25 & closings==0 
local yy = r(mean)
twoway ///
 (pcarrowi `yy' 26.5 `yy' 25  if _n==1, lc(none) mc(none) text(`yy' 26.75 "Excess mass: `bu'" "Standard error: 0`bu_sd'", color(gs2) place(e) just(left))) ///
(connected avg antvars if closings==0, mc(orange_red) lc(orange_red))  ///
(connected avg antvars if closings==1, lp(-) mc(midblue ) lc(midblue ))  /// 
,xline(25, lpattern(dash) lcolor(gs7) lstyle(foreground)) ///
graphregion(color(white)) ytitle("Share of layoffs") xtitle("Number of notified workers") ///
 name(a,replace) graphregion(color(white)) ///
 legend(order(2 "No closings" 3 "Closings") region(lcolor(white)))
graph export "$agegraphpath/Bunching/bunching_1.pdf", replace

clear
use `a0'

keep if inrange(antvars,15,40) 
egen complier2 = xtile(complier) if varselorsak<5, n(4)
replace complier2  = 0 if varselorsak>=5
tab complier2

tempfile b0
save	`b0', replace

tempfile bunch 
foreach num of numlist 1(1)101 {
	di "Run `num'"
	qui {
	clear 
	use `b0'
	
	if `num'>1 {
		bsample //, strata(complier2)
	}
	bys complier2 : gen tot =_N

	collapse (mean) complier (count) varselid , by(antvars complier2 tot)
	sum tot
	gen avg = varselid/tot
	sort complier2 antvars
	gen run = `num'
	if `num'>1 {
		append using `bunch'		
	}
	save `bunch', replace
	}
}

twoway ///
(connected avg antvars if complier2==1 & run==1)  ///
(connected avg antvars if complier2==2 & run==1)  /// 
(connected avg antvars if complier2==3 & run==1)  /// 
(connected avg antvars if complier2==4 & run==1)  /// 
(connected avg antvars if complier2==0 & run==1)  /// 
,xline(25, lpattern(dash) lcolor(maroon) lstyle(foreground)) ///
graphregion(color(white)) ytitle("Share of layoffs") xtitle("Number of notified workers") ///
 graphregion(color(white)) ///
 legend(order(1 "Share with short notice: bottom quartile" 2 "25-50%" 3 "50-75%" 4 "75-100%" 5 "Closings") col(3) region(lcolor(white))) 
graph export "$agegraphpath/Bunching/bunching_2.pdf", replace

keep if inrange(antvars,22,25) 
gen temp = avg if antvars==25 & complier2==0 
egen cf = max(temp), by(run)
collapse (mean) complier (sum) avg, by(complier2 run cf)

gen temp = avg if complier2==0 
egen mtem = max(temp), by(run)
gen excessmass = (avg - mtem)/cf 
drop mtem temp 
gegen sd_mass = sd(excessmass) if run>1, by(complier2)
egen temp=max(sd_mass), by(complier2)
replace sd_mass = temp if mi(sd_mass)
keep if run==1

gen c1 = excessmass - 1.96*sd_mass
gen c2 = excessmass + 1.96*sd_mass

twoway ///
(rcap c1 c2 complier if complier2>0) ///
(scatter excessmass complier if complier2>0 ) ///
, graphregion(color(white)) ///
xt("Share with short notice") yt("Excess mass")  ///
xl(0(0.2)1) legend(off) yl(-1(1)4) yline(0,lc(gs7))
graph export "$agegraphpath/Bunching/bunching_3.pdf",replace

*!gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=F1_Bunching_"$S_DATE".pdf temp_bunching_*.pdf
*!rm -f temp_bunching_*.pdf
*!rm -f                 *.eps





